import torch
import torch.nn as nn
import torch.optim as optim
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN,self).__init__()
        self.fc1=nn.Linear(3,2)
        self.fc2=nn.Linear(2,1)
    def forward(self,x):
        x=torch.relu(self.fc1(x))
        x=self.fc2(x)
        return x
model=SimpleNN()
print(model)

model=SimpleNN()
criterion=nn.MSELoss()#均方差损失函数
optimizer=optim.Adam(model.parameters(),lr=0.001) #adam优化器

X=torch.randn(10,3)
Y=torch.randn(10,1)
for epoch in range(100):
    optimizer.zero_grad() #清空之前的梯度
    output=model(X)#前向传播
    loss=criterion(output,Y)#计算损失
    loss.backward()#反向传播
    optimizer.step()#更新参数
    if (epoch+1)%10 ==0:
        print(f'Epoch [{epoch+1}/100],loss:{loss.item():.4f}')
